using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Microsoft.VisualBasic;
using System.Reflection;
using System.Drawing.Imaging;

namespace Calcio
{
    internal sealed class modFunction
    {
        // Fields
        public static string eseDesc;
        public static string eseTitolo;
        public static string imgPrn;
        public static string nomeIMG;
        public static string nomeXML;

        // Methods
        [DllImport("gdi32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern bool BitBlt(IntPtr hdcDest, int x, int y, int Width, int Height, IntPtr hdcSrc, int XSrc, int YSrc, int dwRop);
        public static void caricaEserc(ListBox ctrl)
        {
            ctrl.Items.Clear();
            DirectoryInfo info = new DirectoryInfo(GetPath() + @"\Esercitazioni\");
            if (!info.Exists)
            {
                Directory.CreateDirectory(GetPath() + @"\Esercitazioni");
            }
            foreach (FileInfo info2 in info.GetFiles("*.ese"))
            {
                ctrl.Items.Add(Strings.Left(info2.Name, Strings.Len(info2.Name) - 4));
            }
        }

        public static void creaMyXML(string percToSave)
        {
            nomeXML = percToSave + @"\" + eseTitolo + ".xml";
            string str = "<?xml version='1.0' ?>\n\r";
            str = ((((((((((str + "<esercitazione>\n\r" + "<titolo>\n\r") + eseTitolo + "\n\r") + "</titolo>\n\r" + "<descrizione>\n\r") + eseDesc + "\n\r") + "</descrizione>\n\r" + "<immagine>\n\r") + eseTitolo + ".jpg\n\r") + "</immagine>\n\r" + "<data>\n\r") + Conversions.ToString(DateTime.Now) + "\n\r") + "</data>\n\r") + "<utente>\n\r" + "\n\r") + "</utente>\n\r" + "</esercitazione>";
            StreamWriter writer = new StreamWriter(nomeXML);
            writer.WriteLine(str);
            writer.Close();
        }

        public static string GetPath()
        {
            string location = Assembly.GetExecutingAssembly().Location;
            return location.Substring(0, location.LastIndexOf(@"\"));
        }

        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern IntPtr GetWindowDC(IntPtr hWnd);
        public static string myCrypt(string str)
        {
            int num2 = Strings.Len(str) - 1;
            string str3 = "";
            int num3 = num2;
            for (int i = 0; i <= num3; i++)
            {
                string expression = Conversions.ToString(Strings.Asc(str.Substring(i, 1)));
                while (Strings.Len(expression) < 3)
                {
                    expression = "0" + expression;
                }
                str3 = str3 + expression;
            }
            return str3;
        }

        public static string myDecrypt(string str)
        {
            int num2 = Strings.Len(str) - 1;
            string str3 = "";
            int num3 = num2;
            for (int i = 0; i <= num3; i++)
            {
                string str4 = Conversions.ToString(Strings.Chr(Conversions.ToInteger(str.Substring(i, 3))));
                i += 2;
                str3 = str3 + str4;
            }
            return str3;
        }

        [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
        public static void saveImgTmp(Control pannello, string tmpPerc)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            Bitmap image = new Bitmap(pannello.Width, pannello.Height);
            Graphics graphics = Graphics.FromImage(image);
            IntPtr windowDC = GetWindowDC(pannello.Handle);
            IntPtr hdc = graphics.GetHdc();
            BitBlt(hdc, 0, 0, pannello.Size.Width, pannello.Size.Height, windowDC, 0, 0, 0xcc0020);
            ReleaseDC(pannello.Handle, windowDC);
            graphics.ReleaseHdc(hdc);
            graphics.Dispose();
            image.Save(imgPrn, ImageFormat.Jpeg);
        }

        [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern void Sleep(long dwMilliseconds);
    }
}
